<%/*
 * Copyright (c) 2007
 *	Alberto Magni, Maggioni Marcello
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */%>
 <%-- 
    Document   : admin_configuration
    Created on : 28-nov-2007, 18.26.52
    Author     : Marcello Maggioni
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="Database.XMLManager.XMLUsersFile" %>
<%@page import="Database.*" %>
<%@page import="java.util.Map"%>
<%@page errorPage="error.jsp" %>
<%@ taglib uri="/WEB-INF/tlds/login" prefix="access" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Administrator Configuration Page</title>
    </head>
    <body>
        <access:LoginTag/>
        <access:AdminTag/>
        <jsp:useBean id="loggedin" type="beans.LoginBean" scope="session"/>
        <jsp:include page="username.jsp"><jsp:param name="username" value="<%= loggedin.getUsername() %>"/></jsp:include>
        <% ConfigurationDB cdb = (ConfigurationDB) application.getAttribute("configurationdb");
           AddressesDB adb = (AddressesDB) application.getAttribute("addressesdb");
           String email = cdb.getEMailAddress(null);
           String server = cdb.getServer(null);
           String originalfilename = adb.getOriginalFilename(loggedin.getUsername());
           boolean spamcheck = cdb.getSpamCheck(null);
           boolean srvauth = cdb.getServerAuthMode(null);
           if (server == null)
               server = "";
           if  (email == null)
               email = "";%>
        <h2>Administrator Configuration Page</h2>
        <hr>
        <p>After password change logout is recommended</p>
        <%--
        <form action="AdminConfigSrv" method=post>
            <h3>Change username</h3>
            <table>
                <tr><td>New Username:</td><td><input type=text name="username"/></tr>
                <tr><td><input type="submit" value="Change"></td></tr>
            </table>
        </form>
        --%>
        <form action="AdminConfigSrv" method=post>
            <h3>Change Password</h3>
            <table>
                <tr><td>New Password:</td><td><input type=password name="passwordone"/></td></tr>
                <tr><td>Retype New Password:</td><td><input type=password name="passwordtwo"/></td></tr>
                <tr><td><input type=submit value="Change"></td></tr>
            </table>
        </form>
        <h3>User Management</h3>
        <table><tr><td>
                    <form action="AdminConfigSrv" method=post>
                        <table>
                            <h4>Disable users</h4>
                            <tr><td><select name="disable" multiple>
                                        <% UsersDB xmuf = null;
            synchronized (application) {
                xmuf = (UsersDB) application.getAttribute("usersdb");
                if (xmuf == null) {
                    xmuf = XMLUsersFile.getDefaultInstance(application.getRealPath(""));
                    application.setAttribute("usersfile", xmuf);
                }
            }
            String[] array = xmuf.getEnabledUsers();
            for (int count = 0; count < array.length; count++) {
                if (!xmuf.isAdmin(array[count])) {
                    String realname = xmuf.getName(array[count]);
                    out.println("<option value=\"" + array[count] + "\">" + array[count] + " - " + realname);
                }
            }
                                        %>
                            </select></td></tr>
                            <tr><td><input type=submit value="Disable User"></td></tr>
                        </table>
                </form></td>
                <td><form action="AdminConfigSrv" method=post>
                    <table>
                        <h4>Enable users</h4>
                        <tr><td><select name="enable" multiple>
                                    <% String[] array2 = xmuf.getDisabledUsers();
            for (int count = 0; count < array2.length; count++) {
                if (!xmuf.isAdmin(array2[count])) {
                    String realname = xmuf.getName(array2[count]);
                    out.println("<option value=\"" + array2[count] + "\">" + array2[count] + " - " + realname);
                }
            }
                                    %>
                        </select></td></tr>
                        <tr><td><input type=submit value="Enable User"></td></tr>
                    </table>
                </form>
                <td><form action="AdminConfigSrv" method=post>
                        <table>
                            <h4>Delete users</h4>
                            <tr><td><select name="delete" multiple>
                                        <%
            for (int count = 0; count < array.length; count++) {
                if (!xmuf.isAdmin(array[count])) {
                    String realname = xmuf.getName(array[count]);
                    out.println("<option value=\"" + array[count] + "\">" + array[count] + " - " + realname);
                }
            }
            for (int count = 0; count < array2.length; count++) {
                if (!xmuf.isAdmin(array2[count])) {
                    String realname = xmuf.getName(array2[count]);
                    out.println("<option value=\"" + array2[count] + "\">" + array2[count] + " - " + realname);
                }
            }
                                        %>
                            </select></td></tr>
                            <tr><td><input type=submit value="Delete User"></td></tr>
                        </table>
                    </form>
        </td></tr></table>
        <h3>Set admin default addresses:</h3>
        <table>
        <form enctype="multipart/form-data" method=post action="AdminConfigSrv" multipart>
            <tr><td><b>Currently Loaded File:</b><%if (originalfilename == null || originalfilename.isEmpty()) {
                out.print(" None");
            } else {
                out.print(" " + originalfilename);
            }%></td></tr>
            <tr><td>File (CSV, LDIF, XML):</td><td><input type=file name="fileupload"/></td>
            <tr><td><input type=submit value="Submit"/></td></tr>
        </form>
        </table>
        <h3>Enable Spamassassin support:</h3>
        <form method=post action="AdminConfigSrv">
            <table>
                <tr><td><input type=checkbox name="spamcheck" <%if (spamcheck) { out.print("checked"); }%> value="enable"/></td> <td>Enable Spamassassin</td></tr>
                <tr><td><input type=submit name="spamcheckbut" value="Set"/></td></tr>
        </table>
        </form>
        <h3>Server Authentication</h3>
        <form method=post action="AdminConfigSrv">
            <table>
                <tr><td><input type=checkbox name="srvauth" <%if (srvauth) { out.print("checked"); }%> value="enable"/></td> <td>Enable Server Authentication through SSL</td></tr>
                <tr><td><input type=submit name="srvauthbut" value="Set"/></td></tr>
        </table>
        </form>
        <h3>Set Public SMTP Server:</h3>
        <table>
        <form method=post action="AdminConfigSrv">
            <tr><td>SMTP Server:</td><td><input type=text name="server" value="<%= server %>"/></td></tr>
            <tr><td><input type=submit value="Set"/></td></tr>
        </form>
        </table>
        <h3>Set E-Mail Address:</h3>
        <table>
        <form method=post action="AdminConfigSrv">
            <tr><td>E-Mail address:</td><td><input type=text name="mail" value="<%= email %>"/></td></tr>
            <tr><td><input type=submit value="Set"/></td></tr>
        </form>
        </table>
        <h3>Download Default Addresses</h3>
        <form method=post action="AdminConfigSrv">
            <input type=submit name="download" value="Download"/>
        </form>
        <form action="datainput.jsp"><input type=submit value="Back"/></form>
        <jsp:include page="logout.jsp"/>
    </body>
</html>
